﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaccaratRally.DataKernel.Entity;
using BaccaratRally.DataKernel.Service;


namespace BaccaratRally.Service.Api
{
    public partial class KeepAlive : System.Web.UI.Page
    {
        public string AccountName;
        public string AccountPos;
        public string CurrSite;
        public Byte TableStatus1;
        public Byte TableStatus2;
        public Byte TableStatus3;

        protected void Page_Load(object sender, EventArgs e)
        {
            AccountName = Request.QueryString["AccountName"];
            AccountPos = Request.QueryString["AccountPos"];
            CurrSite = Request.QueryString["Site"];

            TableStatus1 = Byte.Parse(Request.QueryString["Ts"].Split(new char[] { '|' })[0]);
            TableStatus2 = Byte.Parse(Request.QueryString["Ts"].Split(new char[] { '|' })[1]);
            TableStatus3 = Byte.Parse(Request.QueryString["Ts"].Split(new char[] { '|' })[2]);

            AccountService aservice = ServiceFactory.CreateService<AccountService>();
            aservice.KeepAlive(AccountName, AccountPos, CurrSite, Request.UserHostAddress, TableStatus1, TableStatus2, TableStatus3);


            BetItemService bis = ServiceFactory.CreateService<BetItemService>();
            List<BetItem> betlist1 = bis.NeedSetBetItemList(AccountName, AccountPos, 1);
            List<BetItem> betlist2 = bis.NeedSetBetItemList(AccountName, AccountPos, 2);
            List<BetItem> betlist3 = bis.NeedSetBetItemList(AccountName, AccountPos, 3);

            StringBuilder responseText = new StringBuilder("");

            if (betlist1.Count == 1) responseText.Append(betlist1[0].BetID.ToString() + "-" + betlist1[0].BetDirection + "-" + betlist1[0].BetSize.ToString("F0") + "-" + betlist1[0].TableNo.ToString());
            if (betlist2.Count == 1)
            {
                if (betlist1.Count == 1) responseText.Append("|");
                responseText.Append(betlist2[0].BetID.ToString() + "-" + betlist2[0].BetDirection + "-" + betlist2[0].BetSize.ToString("F0") + "-" + betlist2[0].TableNo.ToString());
            }
            if (betlist3.Count == 1)
            {
                if (betlist1.Count == 1 || betlist2.Count == 1) responseText.Append("|");
                responseText.Append(betlist3[0].BetID.ToString() + "-" + betlist3[0].BetDirection + "-" + betlist3[0].BetSize.ToString("F0") + "-" + betlist3[0].TableNo.ToString());
            }

            if (betlist1.Count == 0 && betlist2.Count == 0 && betlist3.Count == 0) responseText.Append("NoBets");

            Response.Write(responseText.ToString());
            Response.End();
        }
    }
}